package com.eric.code.oauth2;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.eric.code.oauth2.api.ApiOauthController;
import com.eric.code.oauth2.app.AppController;
import com.eric.code.oauth2.common.IndexController;
import com.eric.code.oauth2.intercepter.LoginSessionInterceptor;
import com.eric.code.oauth2.login.LoginController;
import com.eric.code.oauth2.my.MyController;
import com.eric.code.oauth2.user.UserController;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;

import java.sql.Connection;

/**
 * Created by eric on 17-3-21.
 */
public class Config extends JFinalConfig {

    private static Prop p = loadConfig();

    private static Prop loadConfig() {
        try {
            // 优先加载生产环境配置文件
            return PropKit.use("oauth2_config.txt");
        } catch (Exception e) {
            // 找不到生产环境配置文件，再去找开发环境配置文件
            return PropKit.use("oauth2_config_dev.txt");
        }
    }

    public static void main(String []s){
        /**
         * 特别注意：IDEA 之下建议的启动方式，仅比 eclipse 之下少了最后一个参数
         */
        JFinal.start("src/main/webapp", 9999, "/");
    }

    @Override
    public void configConstant(Constants me) {
        me.setDevMode(true);
    }

    @Override
    public void configRoute(Routes me) {
        me.setBaseViewPath("/view");

        me.add("/", IndexController.class);
        me.add("/login", LoginController.class,"/login");
        me.add("/oauth2", ApiOauthController.class,"/oauth2");
        me.add("/app", AppController.class,"/client");
        me.add("/user", UserController.class,"/user");
        me.add("/my", MyController.class,"/my");
    }

    @Override
    public void configEngine(Engine me) {
        me.addSharedFunction("/view/common/_layout.html");
        me.addSharedFunction("/view/common/_layout_no.html");
        me.addSharedFunction("/view/common/_paginate.html");
    }

    @Override
    public void configPlugin(Plugins me) {
        DruidPlugin druidPlugin = getDruidPlugin();
        WallFilter wallFilter = new WallFilter();              // 加强数据库安全
        wallFilter.setDbType("mysql");
        druidPlugin.addFilter(wallFilter);
        druidPlugin.addFilter(new StatFilter());    // 添加 StatFilter 才会有统计数据
        me.add(druidPlugin);

        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
        _MappingKit.mapping(arp);
        me.add(arp);
        if (p.getBoolean("devMode", false)) {
            arp.setShowSql(true);
        }

        me.add(new EhCachePlugin());
    }

    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new LoginSessionInterceptor());
    }

    @Override
    public void configHandler(Handlers me) {

    }

    public static DruidPlugin getDruidPlugin() {
        return new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password").trim());
    }

}
